//  -------------------- iSharePoint -------------------- //
// 
//  Company:	IT Complex, LLC
//  Project:	Smart.Common
//  File name:	CamlQueryBuilderDateRangesOverlapItem.cs
//  Developer:	Solomatov Igor
//  Created:	15.04.2012
// 
//  -------------------- iSharePoint -------------------- //

using System;

namespace Smart.Common.Entities
{
	public class CamlQueryBuilderDateRangesOverlapItem<TExecutorType> : CamlQueryBuilderStatementItem<TExecutorType>
		where TExecutorType : class
	{
		public string FieldStartDate { get; set; }
		public string FieldEndDate { get; set; }
		public string FieldRecurrenceId { get; set; }
		public bool IncludeTimeValue { get; set; }
		public string ValueS { get; set; }
		public DateTime ValueDT { get; set; }

		public CamlQueryBuilderDateRangesOverlapItem(string fieldStartDate, string fieldEndDate, string fieldRecurrenceId, string value)
			: base(fieldStartDate, null, "DateRangesOverlap", null)
		{
			FieldStartDate = fieldStartDate;
			FieldEndDate = fieldEndDate;
			FieldRecurrenceId = fieldRecurrenceId;
			IncludeTimeValue = value.EqIC("now");
			ValueS = value;
		}

		public CamlQueryBuilderDateRangesOverlapItem(string fieldStartDate, string fieldEndDate, string fieldRecurrenceId, bool includeTimeValue, DateTime value)
			: base(fieldStartDate, null, "DateRangesOverlap", null)
		{
			FieldStartDate = fieldStartDate;
			FieldEndDate = fieldEndDate;
			FieldRecurrenceId = fieldRecurrenceId;
			IncludeTimeValue = includeTimeValue;
			ValueDT = value;
		}

		/// <summary>
		/// Return Cache code
		/// </summary>
		public override int GetCacheCode()
		{
			return
				(Op == null ? 0 : Op.GetHashCode()) ^
				(FieldStartDate == null ? 0 : FieldStartDate.GetHashCode()) ^
				(FieldEndDate == null ? 0 : FieldEndDate.GetHashCode()) ^
				(FieldRecurrenceId == null ? 0 : FieldRecurrenceId.GetHashCode()) ^
				(IncludeTimeValue.GetHashCode() << 5) ^
				(ValueS == null ? 0 : ValueS.GetHashCode()) ^
				(ValueDT.IsEmpty() ? 0 : ValueDT.GetHashCode());
		}

		/// <summary>
		/// Format QueryBuilder statement
		/// </summary>
		public override string FormatQuery()
		{
			var fs = (Query == null ? FieldStartDate : Query.Meta.FieldName(FieldStartDate));
			var fe = (Query == null ? FieldEndDate : Query.Meta.FieldName(FieldEndDate));
			var fr = (Query == null ? FieldRecurrenceId : Query.Meta.FieldName(FieldRecurrenceId));

			if (ValueS.IsNullOrEmpty())
			{
				return
					string.Format("<{0}><FieldRef Name=\"{1}\"/><FieldRef Name=\"{2}\"/><FieldRef Name=\"{3}\"/><Value Type=\"DateTime\" IncludeTimeValue=\"{4}\">{5}</Value></{0}>", Op, fs, fe,
								  fr, (IncludeTimeValue ? "TRUE" : "FALSE"), (IncludeTimeValue ? "<Now />" : "<Today />"));
				// SPUtility.CreateISO8601DateTimeFromSystemDateTime(ValueDT) - NOT SUPPORTED!
			}
			else
			{
				return
					string.Format("<{0}><FieldRef Name=\"{1}\"/><FieldRef Name=\"{2}\"/><FieldRef Name=\"{3}\"/><Value Type=\"DateTime\" IncludeTimeValue=\"{4}\"><{5}/></Value></{0}>", Op, fs, fe,
								  fr, (IncludeTimeValue ? "TRUE" : "FALSE"), ValueS);
			}
		}
	}
}